home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 2.9 KB | 129 lines |
- // TCPDevice
- // The TCPDevice gives JFS client applets a way to make connections to
- // arbitrary hosts and ports.
- // An applet reading from /dev/TCP supplies a host and port, to which the
- // TCP device connects. If successful, the device starts a opens a
- // server socket on a random port, sends the port number back to the client,
- // then waits for a connection and ferrys all data to and fro between client
- // and the remote host.
- import java.io.*;
- import java.net.*;
-
- public class TCPDevice extends DeviceDriver
- {
- // read
- // Valid message parameters are:
- // Host: <hostname>
- // Port: <port num>
- Message read(Message msg, ServerClient sc) throws IOException
- {
- String host = msg.find("Host");
- String port = msg.find("Port");
- if (host == null)
- throw new IOException("No Host given");
- if (port == null)
- throw new IOException("No Port given");
-
- // Make the connection
- Socket s = null;
- try s = new Socket(host, Integer.parseInt(port));
- catch(UnknownHostException e)
- throw new IOException("Host "+host+" not found");
- catch(IOException e)
- throw new IOException("Could not connect to "+host);
-
- // Find a free server socket
- ServerSocket ss = null;
- int p = 9888;
- do {
- p++;
- try ss = new ServerSocket(p);
- catch(Exception e);
- } while(ss == null);
-
- // Start up a connection proxy on the port
- new ConnectionProxy(s, ss);
-
- // Reply with the port
- Message r = new Message();
- r.add("Reply", "Success");
- r.add("Port", String.valueOf(p));
- return r;
- }
- }
-
- // ConnectionProxy
- // Given a socket and a server socket, wait for a connection and pass through
- // all data both ways
- class ConnectionProxy implements Runnable
- {
- InputStream rin, cin; // from remote host and client
- OutputStream rout, cout; // to remote host and client
- ServerSocket sock;
- Thread rth, cth;
-
- ConnectionProxy(Socket s, ServerSocket ss)
- {
- try {
- cin = s.getInputStream();
- cout = s.getOutputStream();
- }
- catch(Exception e)
- return; // huh?
- sock = ss;
- rth = new Thread(this, "Remote");
- rth.start();
- }
-
- public void run()
- {
- if (Thread.currentThread().getName().equals("Remote")) {
- // Wait for a connection
- Socket s = null;
- try s = sock.accept();
- catch(Exception e)
- return;
-
- // Get the input and output streams from the new connection
- try {
- rin = s.getInputStream();
- rout = s.getOutputStream();
- }
- catch(Exception e)
- return;
-
- // Begin the client thread
- cth = new Thread(this, "Client");
- cth.start();
-
- // Read from the remote host, and write to the client
- copy(rin, cout);
- }
- else {
- // Read from the client, and write to the remote host
- copy(cin, rout);
- }
- try {
- rin.close();
- rout.close();
- cin.close();
- cout.close();
- }
- catch(IOException e);
- }
-
- // copy
- // Copy all data from an input to an output, until EOF
- void copy(InputStream in, OutputStream out)
- {
- try {
- int c;
- while((c = in.read()) != -1)
- out.write((byte)c);
- }
- catch(IOException e)
- return;
- }
- }
-
-